package com.huaao.micropolice.root.commons.init;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.huaao.micropolice.core.common.Constants;
import com.huaao.micropolice.core.model.system.Menu;
import com.huaao.micropolice.core.service.system.MenuService;
import com.huaao.micropolice.root.commons.CacheUtils;

@Component
public class MenuCache implements ApplicationListener<ContextRefreshedEvent>{
	
	@Autowired
	private MenuService menuService;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		List<Menu> menus = menuService.findAll();
		for(Menu m : menus){
			if(!StringUtils.isBlank(m.getUrl()) && m.getUrl().lastIndexOf("/") != -1){
				String url = m.getUrl();
				String key = url.substring(0, url.lastIndexOf("/"));
				CacheUtils.put(Constants.CACHE_MENU_URL, key, m.getId());
			}
		}
	}
}
